//
//  PasswordInputView.m
//  test
//
//  Created by MT on 16/10/18.
//  Copyright © 2016年 MT. All rights reserved.
//

#import "PasswordInputView.h"

@interface PasswordInputView ()

@property (nonatomic, weak) UITextField *textField;

@end

@implementation PasswordInputView

#pragma mark - Singleton

+ (PasswordInputView *)shareInstance {
    static id instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[self alloc] initWithFrame:[UIScreen mainScreen].bounds];
    });
    return instance;
}

#pragma mark - Initilize

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setup];
    }
    return  self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
        [self setup];
    }
    return self;
}

- (void)setup {
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 50, 200, 20)];
    label.text = @"请输入密码";
    [self addSubview:label];
    
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 80, 200, 20)];
    textField.backgroundColor = [UIColor whiteColor];
    textField.secureTextEntry = YES;
    [self addSubview:textField];
    self.textField = textField;
    
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10, 110, 200, 44)];
    [button setBackgroundColor:[UIColor blueColor]];
    [button setTitle:@"确定" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(completeButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:button];
    
    self.backgroundColor = [UIColor yellowColor];
}

#pragma mark - Common Methods

- (void)completeButtonPressed:(UIButton *)button {
    if ([self.textField.text isEqualToString:@"123456"]) {
        [self.textField resignFirstResponder];
        [self resignKeyWindow];
        self.hidden = YES;
    } else {
        [self showErrorAlertView];
    }
}

- (void)showErrorAlertView {
    NSLog(@"密码输入错误");
}


- (void)show {
    [self makeKeyWindow];
    self.hidden = NO;
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
